var pagNum1 = 0;
var preCateId1 = '';
var userId = userid;
jQuery(function () {

    $("#toLogin").click(function(){
        window.location.href = '/LianniuWeb/login.do';
    })
});
var b = false;
var loadProductRemote = function (cateId) {
    if (b) {
        return;
    }
    var searchName = $("#searchName").val();
    if(pagNum1 == 0){
        $("#prodlist_index").children().remove()
    }
    b = true;
    $.ajax({
        url: server_url + "buyer/product/rest/getProductList.do?cateId=" + cateId + "&userId=" + userid + "&currPage=" + pagNum1,
        data:{searchName:searchName,random:Math.random()},
        type: 'get',
        ansyc: false,
        error: function () {
            alert(contants.systemError)
        },
        success: function (msg) {
            var data = msg.data;
            b = false;
            dealProductData(data);
        }
    });
}
var dealProductData = function (data) {
    var s = '';
    $("#more").remove();
    if (data != null && data.length > 0) {
        for (var i = 0; i < data.length; i++) {
            s += '<div class="box_r" onclick="setSku(' + data[i].id + ');">\
            <div class="display_block">\
            <div class="det_box">\
            <img src="' + data[i].picturePath1 + '" alt="" class="">\
            <div class="">\
            <div class="con_tit">\
            <p>' + data[i].productName + '</p>\
            <span>¥' + data[i].price + '/头</span>\
            </div>\
            <p class="con_normal">类别：' + data[i].specName + '</p>\
            <p class="con_normal">库存：' + data[i].overPlusNum + '头</p>\
            <p class="con_normal">货源地：' + data[i].areaName + '</p>\
            <p class="con_normal">编号：' + data[i].id + '</p>\
            <p class="con_normal">'+ data[i].addTime +'</p>\
            </div>\
            </div>\
            </div>\
            </div>';
        }
        $("#prodlist_index").append(s);
        if(userid == null || userid == ''){
            $("#toLogin").show();
        }else{
            var more = '<div class="det_box" id="more" style="display: block" onclick="loadProductRemote();"> \
            <p style="\
        text-align: center;margin-left: -30px;;\
        ">加载更多</p>\
        </div>';
            $("#prodlist_index").append(more);
        }


        pagNum1++;
    } else {
        if (pagNum1 < 1) {
            s = '<div class="box_r"><div class="display_block"><div class="det_box" id="more" style="display: block"> \
            <p style="\
        text-align: center;margin-left: -30px;;\
        ">没有相关商品</p>\
        </div></div></div>';
            $("#prodlist_index").html(s);
        }else{
            var more = '<div class="box_r"><div class="display_block"><div class="det_box" id="more" style="display: block"> \
            <p style="\
        text-align: center;margin-left: -30px;;\
        ">没有更多了</p>\
        </div></div></div>';
            $("#prodlist_index").append(more);
        }

    }

}
var setSku = function(skuId){
    localStorage.setItem('skuId', skuId);
    window.location.href=server_url+"buyer/product/productDetail.do";
}

var search = function(){
    var searchName = $("#searchName").val();
    if(searchName == null || searchName == ''){
        return;
    }
    pagNum1 = 0;
    var cateId = '';
    loadProductRemote(cateId)
}
document.onkeydown = function (event){
    if (event.keyCode==13) //回车键的键值为13
        search();
};